package com.example.elmmerchants.Message_Setting;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.Ringtone_Setting.Get_Order_Ringtone;
import com.example.elmmerchants.Ringtone_Setting.Ringtone_Try_listen;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Message_Ringtone_Page extends AppCompatActivity {

    Switch is_open_first_time_reminder_switch, is_open_one_min_after_reminder_switch, is_open_three_min_after_reminder_switch;
    LinearLayout play_first_time_reminder, play_one_time_reminder, play_three_time_reminder;

    String email;
    int is_open_first_reminder, is_open_one_min_after_reminder, is_open_three_min_after_reminder, how_many_volume;
    AudioManager mAudio;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_ringtonr_page);

        mAudio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

        email = getIntent().getStringExtra("email");
        is_open_first_reminder = getIntent().getIntExtra("is_open_first_reminder", 0);
        is_open_one_min_after_reminder = getIntent().getIntExtra("is_open_one_min_after_reminder", 0);
        is_open_three_min_after_reminder = getIntent().getIntExtra("is_open_three_min_after_reminder", 0);
        how_many_volume = getIntent().getIntExtra("how_many_volume", 0);

        is_open_first_time_reminder_switch = findViewById(R.id.is_open_first_time_reminder);
        is_open_one_min_after_reminder_switch = findViewById(R.id.is_open_one_min_after_reminder);
        is_open_three_min_after_reminder_switch = findViewById(R.id.is_open_three_min_after_reminder);
        play_first_time_reminder = findViewById(R.id.play_first_time_reminder);
        play_one_time_reminder = findViewById(R.id.play_one_time_reminder);
        play_three_time_reminder = findViewById(R.id.play_three_time_reminder);

        mAudio.setStreamVolume(AudioManager.STREAM_MUSIC, how_many_volume, AudioManager.FLAG_PLAY_SOUND);

        if (is_open_first_reminder == 1) {
            is_open_first_time_reminder_switch.setChecked(true);
        }
        else {
            is_open_first_time_reminder_switch.setChecked(false);
        }

        if (is_open_one_min_after_reminder == 1) {
            is_open_one_min_after_reminder_switch.setChecked(true);
        }
        else {
            is_open_one_min_after_reminder_switch.setChecked(false);
        }

        if (is_open_three_min_after_reminder == 1) {
            is_open_three_min_after_reminder_switch.setChecked(true);
        }
        else {
            is_open_three_min_after_reminder_switch.setChecked(false);
        }

        is_open_first_time_reminder_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_first_time_reminder_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_first_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_first_time_reminder_switch.setChecked(true);
                            is_open_first_reminder = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_first_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_first_time_reminder_switch.setChecked(false);
                            is_open_first_reminder = 0;
                        });
                    });
                }
            }
        });

        is_open_one_min_after_reminder_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_one_min_after_reminder_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_one_min_after_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_one_min_after_reminder_switch.setChecked(true);
                            is_open_one_min_after_reminder = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_one_min_after_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_one_min_after_reminder_switch.setChecked(false);
                            is_open_one_min_after_reminder = 0;
                        });
                    });
                }
            }
        });

        is_open_three_min_after_reminder_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (is_open_three_min_after_reminder_switch.isChecked()) {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_three_min_after_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 1);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_three_min_after_reminder_switch.setChecked(true);
                            is_open_three_min_after_reminder = 1;
                        });
                    });
                }
                else {
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql = "update `merchant_message_setting` set is_open_three_min_after_reminder = ? where email = ?";
                            PreparedStatement preparedStatement = connection.prepareStatement(sql);
                            preparedStatement.setInt(1, 0);
                            preparedStatement.setString(2, email);
                            preparedStatement.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            is_open_three_min_after_reminder_switch.setChecked(false);
                            is_open_three_min_after_reminder = 0;
                        });
                    });
                }
            }
        });

        play_first_time_reminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Message_Ringtone_Page.this, Message_Ringtone.class);
                stopService(intent);
                intent.putExtra("message_ringtone", "首次提醒");
                startService(intent);
            }
        });

        play_one_time_reminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Message_Ringtone_Page.this, Message_Ringtone.class);
                stopService(intent);
                intent.putExtra("message_ringtone", "1分钟未回复提醒");
                startService(intent);
            }
        });

        play_three_time_reminder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Message_Ringtone_Page.this, Message_Ringtone.class);
                stopService(intent);
                intent.putExtra("message_ringtone", "3分钟未回复提醒");
                startService(intent);
            }
        });
    }
}